<?php
$pageTitle = '首页';
require_once 'config/config.php';
require_once 'classes/User.php';
require_once 'classes/Task.php';
require_once 'classes/Friend.php';

// 创建必要的目录
if (!file_exists(UPLOAD_PATH)) {
    mkdir(UPLOAD_PATH, 0755, true);
}
if (!file_exists(AVATAR_PATH)) {
    mkdir(AVATAR_PATH, 0755, true);
}

// 检查是否有记住我的cookie
if (!isLoggedIn() && isset($_COOKIE['remember_token'])) {
    $token = $_COOKIE['remember_token'];
    $db = Database::getInstance();
    $user = $db->fetch("SELECT * FROM users WHERE remember_token = ?", [$token]);
    
    if ($user) {
        $_SESSION['user_id'] = $user['id'];
    }
}

include 'includes/header.php';

// 如果用户已登录，显示个性化内容
if (isLoggedIn()) {
    $userId = $_SESSION['user_id'];
    $user = getUserInfo($userId);
    
    // 获取任务列表
    $task = new Task();
    $pendingTasks = $task->getUserTasks($userId, 'pending');
    $completedTasks = $task->getUserTasks($userId, 'completed');
    $expiredTasks = $task->getUserTasks($userId, 'expired');
    
    // 获取男友列表
    $friend = new Friend();
    $friendRequests = $friend->getFriendRequests($userId);
    $friends = $friend->getFriends($userId);
    
    // 统计数据
    $pendingTaskCount = count($pendingTasks);
    $completedTaskCount = count($completedTasks);
    $expiredTaskCount = count($expiredTasks);
    $friendCount = count($friends);
    $requestCount = count($friendRequests);
?>

<div class="container py-4">
    <!-- 欢迎区域 -->
    <div class="row">
        <div class="d-flex align-items-center">
            <img src="<?php echo getUserAvatar($user['avatar']); ?>" alt="头像" class="avatar-md rounded-circle shadow">
            <div class="ms-3">
                <h3 class="mb-1">你好，<?php echo htmlspecialchars($user['nickname'] ?? $user['username']); ?>！</h3>
                <p class="text-muted mb-0">今天是 <?php echo date('Y年m月d日'); ?>，祝您有个愉快的一天！</p>
            </div>
        </div>
    </div>
    
    <!-- 统计卡片 -->
    <div class="row">
        <div class="col-md-3 col-6 mb-3 mb-md-0">
            <div class="card shadow-sm border-0 rounded-3 h-100">
                <div class="card-body text-center">
                    <div class="display-4 text-primary mb-2"><?php echo $pendingTaskCount; ?></div>
                    <h5 class="card-title">待处理事务</h5>
                    <a href="views/task/list.php?status=pending" class="stretched-link"></a>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6 mb-3 mb-md-0">
            <div class="card shadow-sm border-0 rounded-3 h-100">
                <div class="card-body text-center">
                    <div class="display-4 text-success mb-2"><?php echo $completedTaskCount; ?></div>
                    <h5 class="card-title">已完成事务</h5>
                    <a href="views/task/list.php?status=completed" class="stretched-link"></a>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6 mb-md-0">
            <div class="card shadow-sm border-0 rounded-3 h-100">
                <div class="card-body text-center">
                    <div class="display-4 text-info mb-2"><?php echo $friendCount; ?></div>
                    <h5 class="card-title">男友数量</h5>
                    <a href="views/friend/list.php" class="stretched-link"></a>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-6 mb-md-0">
            <div class="card shadow-sm border-0 rounded-3 h-100">
                <div class="card-body text-center">
                    <div class="display-4 <?php echo $requestCount > 0 ? 'text-warning' : 'text-muted'; ?> mb-2"><?php echo $requestCount; ?></div>
                    <h5 class="card-title">男友请求</h5>
                    <a href="views/friend/request.php" class="stretched-link"></a>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 主要内容区 -->
    <div class="row">
        <!-- 左侧：待处理事务 -->
        <div class="mb-lg-0">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center py-3">
                    <h5 class="mb-0"><i class="fas fa-tasks me-2"></i>待处理事务</h5>
                    <div>
                        <a href="views/task/create.php" class="btn btn-sm btn-light"><i class="fas fa-plus me-1"></i>新建事务</a>
                    </div>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($pendingTasks)): ?>
                        <div class="text-center py-5">
                            <i class="fas fa-clipboard-check fa-3x text-muted mb-3"></i>
                            <p class="lead">暂无待处理事务</p>
                        </div>
                    <?php else: ?>
                        <div class="list-group list-group-flush">
                            <?php foreach (array_slice($pendingTasks, 0, 5) as $task): ?>
                                <a href="views/task/detail.php?id=<?php echo $task['id']; ?>" class="list-group-item list-group-item-action p-3">
                                    <div class="d-flex w-100 justify-content-between align-items-center">
                                        <h5 class="mb-1"><?php echo htmlspecialchars($task['title']); ?></h5>
                                        <span class="badge bg-<?php echo strtotime($task['deadline']) - time() < 86400 ? 'danger' : 'primary'; ?> rounded-pill">
                                            <?php echo getRemainingTime($task['deadline']); ?>
                                        </span>
                                    </div>
                                    <p class="mb-1 text-muted"><?php echo htmlspecialchars(substr($task['description'], 0, 80) . (strlen($task['description']) > 80 ? '...' : '')); ?></p>
                                    <div class="d-flex align-items-center mt-2">
                                        <small class="text-muted me-3"><i class="far fa-calendar-alt me-1"></i><?php echo formatDateTime($task['deadline']); ?></small>
                                        <small class="text-muted">
                                            <i class="fas fa-users me-1"></i>
                                            <?php 
                                            $taskObj = new Task();
                                            $participants = $taskObj->getTaskParticipants($task['id']);
                                            $names = array_map(function($p) {
                                                return htmlspecialchars($p['nickname'] ?? $p['username']);
                                            }, $participants);
                                            echo implode(', ', $names);
                                            ?>
                                        </small>
                                    </div>
                                </a>
                            <?php endforeach; ?>
                        </div>
                        <?php if (count($pendingTasks) > 5): ?>
                            <div class="card-footer bg-white text-center py-3">
                                <a href="views/task/list.php" class="btn btn-outline-primary">查看全部事务</a>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
} else {
    // 未登录用户显示欢迎页面
?>

<div class="container py-3">
    <div class="row-flex justify-content-center">
        <div class="col-lg-10">
            <div class="card shadow border-0 rounded-3 overflow-hidden">
                <div class="row-flex g-0">
                    <div class="col-md-6 bg-primary text-white p-5 d-flex flex-column justify-content-center">
                        <h1 class="display-4 fw-bold mb-4">男友事务预约</h1>
                        <p class="lead mb-4">一个帮助您管理与男友之间事务的平台，添加男友、约定事务、增加亲密度。</p>
                        <div class="d-grid gap-3 d-sm-flex">
                            <a href="login.php" class="btn btn-light btn-lg px-4"><i class="fas fa-sign-in-alt me-2"></i>登录</a>
                            <a href="register.php" class="btn btn-outline-light btn-lg px-4"><i class="fas fa-user-plus me-2"></i>注册</a>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="p-5">
                            <h2 class="fw-bold mb-4">主要功能</h2>
                            <div class="mb-4">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="feature-icon bg-primary text-white rounded p-4 me-4">
                                        <i class="fas fa-user-friends"></i>
                                    </div>
                                    <h4 class="mb-0">男友管理</h4>
                                </div>
                                <p class="text-muted">添加男友，管理男友列表，查看男友资料和亲密度。</p>
                            </div>
                            <div class="mb-4">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="feature-icon bg-success text-white rounded p-4 me-4">
                                        <i class="fas fa-tasks"></i>
                                    </div>
                                    <h4 class="mb-0">事务管理</h4>
                                </div>
                                <p class="text-muted">创建事务，邀请男友参与，设置截止时间，增加亲密度。</p>
                            </div>
                            <div class="mb-4">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="feature-icon bg-info text-white rounded p-4 me-4">
                                        <i class="fas fa-mobile-alt"></i>
                                    </div>
                                    <h4 class="mb-0">移动优化</h4>
                                </div>
                                <p class="text-muted">响应式设计，在手机、平板和电脑上都能完美展示。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
}

include 'includes/footer.php';
?>
